package com.wfuhui.modules.order.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.wfuhui.modules.order.entity.FinanceDetail;
import com.wfuhui.modules.order.entity.FinanceGroup;
import com.wfuhui.modules.order.entity.FinanceUser;
import com.wfuhui.modules.order.entity.FinanceUserRefund;
import com.wfuhui.modules.member.service.MemberService;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wfuhui.modules.order.service.OrderService;
import com.wfuhui.modules.store.service.StoreService;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;

import com.wfuhui.common.utils.Query;
import com.wfuhui.common.utils.R;




/**
 * 订单
 * 
 * @author xiaotian
 * @email codemax@qq.com
 * @date 07-14 13:43:12
 */
@RestController
@RequestMapping("/mall/finance")
public class FinanceController {
	@Autowired
	private OrderService orderService;
	@Autowired
	private StoreService storeService;
	@Autowired
	private MemberService memberService;

	@RequestMapping("/groupExportExcel")
	public void groupExportExcel(@RequestParam Map<String, Object> params, HttpServletResponse resp) throws IOException {
		Query query = new Query(params);
		query.remove("offset");
		List<FinanceGroup> orderList = orderService.queryGroup(query);
		Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("团长统计", "团长"), FinanceGroup.class, orderList);
		resp.setHeader("Content-disposition", "团长统计.xls");
		resp.setContentType("application/vnd.ms-excel");
		resp.setHeader("Content-disposition", "attachment;filename=order.xls");
		resp.setHeader("Pragma", "No-cache");
		workbook.write(resp.getOutputStream());
	}
	
	/**
	 * 列表
	 */
	@RequestMapping("/group")
	public R group(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<FinanceGroup> orderList = orderService.queryGroup(query);
		int total = storeService.queryTotal(query);
		
		return R.ok().put("rows", orderList).put("total", total);
		
	}


	@RequestMapping("/userExportExcel")
	public void userExportExcel(@RequestParam Map<String, Object> params, HttpServletResponse resp) throws IOException {
		Query query = new Query(params);
		query.remove("offset");
		List<FinanceUser> orderList = orderService.queryUser(query);
		Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("会员统计", "统计"), FinanceUser.class, orderList);
		resp.setHeader("Content-disposition", "会员统计.xls");
		resp.setContentType("application/vnd.ms-excel");
		resp.setHeader("Content-disposition", "attachment;filename=order.xls");
		resp.setHeader("Pragma", "No-cache");
		workbook.write(resp.getOutputStream());
	}
	/**
	 * 列表
	 */
	@RequestMapping("/user")
	public R user(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<FinanceUser> orderList = orderService.queryUser(query);
		int total = memberService.queryTotal(query);
		
		return R.ok().put("rows", orderList).put("total", total);
	}

	@RequestMapping("/userRefundExportExcel")
	public void userRefundExportExcel(@RequestParam Map<String, Object> params, HttpServletResponse resp) throws IOException {
		Query query = new Query(params);
		query.remove("offset");
		List<FinanceUserRefund> orderList = orderService.queryUserRefund(query);
		Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("退款统计", "统计"), FinanceUserRefund.class, orderList);
		resp.setHeader("Content-disposition", "退款统计.xls");
		resp.setContentType("application/vnd.ms-excel");
		resp.setHeader("Content-disposition", "attachment;filename=order.xls");
		resp.setHeader("Pragma", "No-cache");
		workbook.write(resp.getOutputStream());
	}
	/**
	 * 列表
	 */
	@RequestMapping("/userRefund")
	public R userRefund(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);

		List<FinanceUserRefund> orderList = orderService.queryUserRefund(query);
		int total = orderService.queryTotalUserRefund(params);

		return R.ok().put("rows", orderList).put("total", total);
	}

	@RequestMapping("/userDetail")
	public R userDetail(@RequestParam Map<String, Object> params){
		if(params.get("statuses") != null && !"".equals(params.get("statuses").toString())) {
			params.put("statuses", params.get("statuses").toString().split(","));
		}
		
		//查询列表数据
		Query query = new Query(params);

		List<FinanceDetail> orderList = orderService.queryFinanceList(params);
		int total = orderService.queryTotal(params);

		return R.ok().put("rows", orderList).put("total", total);
	}

	@RequestMapping("/userDetailExportExcel")
	public void userDetailExportExcel(@RequestParam Map<String, Object> params, HttpServletResponse resp) throws IOException {
		params.put("statuses", new Integer[] {2, 3, 4, 5});
		Query query = new Query(params);
		query.remove("offset");
		List<FinanceDetail> orderList = orderService.queryFinanceList(query);
		Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("公司资金明细", "统计"), FinanceDetail.class, orderList);
		resp.setHeader("Content-disposition", "公司资金明细.xls");
		resp.setContentType("application/vnd.ms-excel");
		resp.setHeader("Content-disposition", "attachment;filename=order.xls");
		resp.setHeader("Pragma", "No-cache");
		workbook.write(resp.getOutputStream());
	}
	
}
